﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Library.Common;
using Library.Bll;

namespace HanhuaMicroCredit.BLL
{
    /// <summary>
    /// IP管理
    /// </summary>
    public static class SetIpRouteSites
    {
        /// <summary>
        /// 获取所有站点的名称及id
        /// </summary>
        /// <returns></returns>
        public static List<Dictionary<string, object>> GetWebSite()
        {
            string tname = "Sys_WebSite";
            string cols = "id, WebSiteName AS name";
            string conditions = "IsValid=1 AND WebSiteState=0";
            List<Dictionary<string, object>> list = CommonBLL.GetList(Database.Default, tname, cols, "id", conditions);
            if (list == null)
                list = new List<Dictionary<string, object>>();
            return list;
        }
        /// <summary>
        /// 返回Dictionary[id,name]形式的站点信息
        /// </summary>
        /// <returns>Dictionary[string,string]</returns>
        public static Dictionary<string, string> GetWebSiteDic()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            List<Dictionary<string, object>> list = GetWebSite();
            foreach (Dictionary<string, object> item in list)
            {
                dic.Add(item["id"].ToString(), item["name"].ToString());
            }
            return dic;
        }
        /// <summary>
        /// 将IP地址转换为整数
        /// </summary>
        /// <param name="ipAddress"></param>
        /// <returns></returns>
        public static long IpToNumber(string ipAddress)
        {
            string[] ips = ipAddress.Split('.');
            if (ips.Length != 4)
            {
                return 0;
            }

            return (long.Parse(ips[0]) << 24) + (long.Parse(ips[1]) << 16) + (long.Parse(ips[2]) << 8) + long.Parse(ips[3]);
        }
    }
}
